package com.hkts.management.message.controller;

import cn.hutool.core.util.RandomUtil;
import com.hkts.management.common.mq.*;
import com.hkts.management.common.utils.BeanConvertUtils;
import com.hkts.management.message.msg.sendUtil.NettySendUtil;
import com.hkts.management.message.utils.SpringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {
	@Autowired
	private AmqpTemplate amqpTemplate;
	@Autowired
	private StringRedisTemplate redisTemplate;

	@Value("${netty.port}")
	private int port;
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mav=new ModelAndView("socket");
		mav.addObject("uid", RandomUtil.randomNumbers(6));
		mav.addObject("port",port);
		return mav;
	}
	@GetMapping("/sendFanoutMessage")
	@ResponseBody
	public String sendFanoutMessage() {
//		String messageId = String.valueOf(SnowFlakeUtil.getFlowIdInstance());
//		String messageData = "message: testFanoutMessage ";
//		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//		Map<String, Object> map = new HashMap<>();
//		map.put("messageId", messageId);
//		map.put("messageData", messageData);
//		map.put("createTime", createTime);
//		amqpTemplate.convertAndSend("fanoutExchange", null, map);
		//发送消息
//		String address="石家庄-邢台";
//		Map<String, Object> params=new HashMap<>(2);
//		params.put("name", "货主");
//		params.put("time", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
//		params.put("address",address);
//		MsgExtras msgExtras=new MsgExtras();
//		msgExtras.setOrderId("406906393799430144");
//		IMessage msg = new IMessage.Builder()
//				.messageId(IMessage.DRAWUP_CONTRACT)
//				.fromUserId("userid")
//				.toUserId("397491981569232897")
//				.chatType(ChatType.UNICAST.getIndex())
//				.msgType(MsgType.JUMPTO.getIndex())
//				.param(params)
//				.extras(msgExtras)
//				.build();
//		Map map= BeanConvertUtils.objectToMap(msg);
//		amqpTemplate.convertAndSend(FanoutRabbitConfig.FANOUTEXCHANGE, FanoutRabbitConfig.ASSIFN, map);
//		HontUserTo hontUserTo = feignInterface.remoteUserInfo();
		String address="北京市东城区-天津市和平区";
		Map<String, Object> params=new HashMap<>();
		params.put("name","白菜");
		params.put("address",address);
		MsgExtras msgExtras=new MsgExtras();
		msgExtras.setOrderId("3");
		IMessage msg = new IMessage.Builder()
				.messageId(IMessage.DRIVER_ASSIGN)
				.fromUserId("263755105029853186")
				.toUserId("397491981569232897")
				.mineType(MineType.TEMPLATE.getIndex())
				.chatType(ChatType.UNICAST.getIndex())
				.msgType(MsgType.JUMPTO.getIndex())
				.param(params)
				.extras(msgExtras)
				.build();
		Map<?,?> msgMap= BeanConvertUtils.objectToMap(msg);
		amqpTemplate.convertAndSend(FanoutRabbitConfig.FANOUTEXCHANGE, FanoutRabbitConfig.ASSIFN, msgMap);
		return "ok";
	}

}
